package com.midea.ai.air.ui.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.ConsVal;
import com.midea.ai.air.ui.home.data.AdviceGridItem;

import java.util.List;

/**
 * Created by ouyangdi on 2016/3/31.
 * PopupWindow中ListView对应的Adapter
 */
public class PopListAdapter extends ArrayAdapter<AdviceGridItem>{
    private LayoutInflater mInflater;

    private int mDividerColor;   //分割线的颜色
    private int mTextColor;   //字体的颜色

    public PopListAdapter(Context context, int resource, List<AdviceGridItem> items) {
        super(context, resource, items);
        mInflater = LayoutInflater.from(context);
    }

    public void setCurrentPage(int currentPage) {
        switch (currentPage) {
            case ConsVal.ACTIVITY_TAG_HOME:
                mDividerColor = getContext().getResources().getColor(R.color.air_home_pop_divider);
                mTextColor = getContext().getResources().getColor(R.color.air_home_pop_text);
                break;
            case ConsVal.ACTIVITY_TAG_DEVICE:
                mDividerColor = getContext().getResources().getColor(R.color.air_device_pop_divider);
                mTextColor = getContext().getResources().getColor(R.color.air_device_pop_text);
                break;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        AdviceGridItem item = getItem(position);
        ViewHolder holder;

        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.item_popupwindow, parent, false);
            holder = new ViewHolder();

            holder.ivIcon = (ImageView)convertView.findViewById(R.id.iv_pop_icon);

            holder.tvState = (TextView)convertView.findViewById(R.id.tv_pop_text);
            holder.tvState.setTextColor(mTextColor);

            holder.viewBotLine = convertView.findViewById(R.id.bottom_line);
            holder.viewBotLine.setBackgroundColor(mDividerColor);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.ivIcon.setImageResource(item.getResIdIcon());
        holder.tvState.setText(item.getStrState());

        if(position == getCount()-1) {
            holder.viewBotLine.setVisibility(View.INVISIBLE);
        } else {
            holder.viewBotLine.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        public ImageView ivIcon;
        public TextView tvState;
        public View viewBotLine;
    }
}
